﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using data;
using icsms.Helpers;

namespace icsms.Areas.Admin.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(FormCollection form)
        {
            try
            {
                string email = form["email"];
                string password = Security.Hash(form["password"]);

                icmsEntities db = new icmsEntities();
                var result = (from user in db.Users
                              where
                                 user.Email == email
                                 &&
                                 user.Password == password
                              select user).FirstOrDefault();

                if (result == null)
                {
                    ViewBag.Message = "Invalid email or password";
                }
                else
                {
                    Session["UserEmail"] = email;
                    Session["IsAutenticated"] = true;
                    Session["UserFullName"] = result.FullName;
                    Session["UserId"] = result.UserId;
                    Session["UserRoleId"] = result.RoleId;
                    return Redirect("~/Admin/Dashboard");
                }
            }
            catch (Exception ex)
            {

            }

            return View("~/Areas/Admin/Views/account/login.cshtml");
        }

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Account/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Account/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Account/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                string fullName = collection["FullName"];
                string email = collection["Email"];
                string password = collection["Password"];

                icmsEntities db = new icmsEntities();
                db.Users.AddObject(new User()
                {
                    FullName = fullName,
                    Email = email,
                    Password = Security.Hash(password),
                    RoleId = 4,
                    CreatedOn = DateTime.Now
                });

                int result = db.SaveChanges();
                if (result > 0)
                {
                    ViewBag.Message = "User created successfully";
                    return View();
                }

            }
            catch(Exception ex)
            {
                ViewBag.Message = "Unable to create user: " + ex.Message;
            }

            return View();
        }

        //
        // GET: /Account/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Account/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Account/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Account/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
